package com.interview.controller;

import com.interview.feign.FeignInterviewService;
import com.interview.pojo.Catalogue;
import com.interview.utils.JsonTurnUtils;
import com.interview.utils.Result;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/feign_catalogue")
@DefaultProperties(defaultFallback = "payment_Global_FallbackMethod")
public class FeignCatalogueController {

    @Autowired
    private FeignInterviewService feignCatalogueService;

    //http://localhost:8083/feign_catalogue
    @PostMapping
    @HystrixCommand
    public String add(@RequestBody Catalogue catalogue){

       feignCatalogueService.add(catalogue);
       return  "添加成功";
    }

    //http://localhost:8083/feign_catalogue/del
    @PostMapping("/del")
    @HystrixCommand
    public String del(@RequestBody Catalogue catalogue){
        feignCatalogueService.del(catalogue);
        return  "删除成功";
    }


    //http://localhost:8083/feign_catalogue/up
    @PostMapping("/up")
    public String up(@RequestBody Catalogue catalogue) {
        feignCatalogueService.catalogu_up(catalogue);
        return "修改成功";
    }



    //http://localhost:8083/feign_catalogue?titleId=60-2021vB1cG6iW10mU38jC34w779R-668
    @GetMapping
    @HystrixCommand
    public String  queryAll(String titleId){
        Result<List<Catalogue>> catalogueResult = feignCatalogueService.queryAll(titleId);
        String s = JsonTurnUtils.objTurnJson( catalogueResult);
        return s;
    }

    //http://localhost:8083/feign_catalogue/all
    @GetMapping("/all")
    public String query_select_all(){
        Result<List<Catalogue>> catalogueResult = feignCatalogueService.query_select_all();
        String s = JsonTurnUtils.objTurnJson( catalogueResult);
        return s;
    }
    //http://localhost:8083/feign_catalogue/queryID?catalogueId=369-2021xB1lR7fS10eE3zV55i949K-722
    @GetMapping("/queryID")
    public String cata_queryID( @RequestParam("catalogueId")  String catalogueId){
        Result<Catalogue> catalogueResult = feignCatalogueService.cata_queryID(catalogueId);
        String s = JsonTurnUtils.objTurnJson( catalogueResult);
        return s;
    }
    //http://localhost:8083/feign_catalogue/count
    @GetMapping("/count")
    public String  catalogue_count(){
        int i = feignCatalogueService.catalogue_count();
        String s = JsonTurnUtils.objTurnJson( i);
        return s;
    }


    //降级方法
    public String payment_Global_FallbackMethod() {
        String str = "<div style=\"width: 100%;text-align: center\">\n" +
                "    <h1 >对不起，网络太拥挤了！ 刷新重试-----Feign</h1>\n" +
                "</div>";
        return str;
    }






    //http://localhost:8083/feign_article/hystrix
    //测试 熔断是否触发
    @GetMapping("/hystrix")
    @ResponseBody
    @HystrixCommand
    public String testHystrix() throws InterruptedException {
        //这个触发了 feign-service 内的 超时处理
       Thread.sleep(5000);

       return  "超时";
    }

}
